<template>
    <view class="container">
        <view class="pay-result">
            <view class="success" v-if="status">
                <view class="msg">付款成功</view>
                <view class="btns">
                    <navigator class="btn" url="/pages/ucenter/order/order" open-type="redirect">查看订单</navigator>
                    <navigator class="btn" url="/pages/index/index" open-type="switchTab">继续逛</navigator>
                </view>
            </view>
            <view class="error" v-if="!status">
                <view class="msg">付款失败</view>
                <view class="tips">
                    <view class="p">
                        请在
                        <text class="time">半小时</text>
                        内完成付款
                    </view>
                    <view class="p">否则订单将会被系统取消</view>
                </view>
                <view class="btns">
                    <navigator class="btn" url="/pages/ucenter/order/order" open-type="redirect">查看订单</navigator>
                    <view class="btn" @tap="payOrder">重新付款</view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
var util = require('../../utils/util.js');

var api = require('../../config/api.js');

var app = getApp();
export default {
    data() {
        return {
            status: false,
            orderId: 0
        };
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.setData({
            orderId: options.orderId,
            status: options.status === '1' ? true : false
        });
    },
    onReady: function () {},
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    methods: {
        payOrder() {
            let that = this;
            util.request(
                api.OrderPrepay,
                {
                    orderId: that.orderId
                },
                'POST'
            ).then(function (res) {
                if (res.errno === 0) {
                    const payParam = res.data;
                    console.log('支付过程开始');
                    uni.requestPayment({
                        timeStamp: payParam.timeStamp,
                        nonceStr: payParam.nonceStr,
                        package: payParam.packageValue,
                        signType: payParam.signType,
                        paySign: payParam.paySign,
                        success: function (res) {
                            console.log('支付过程成功');
                            that.setData({
                                status: true
                            });
                        },
                        fail: function (res) {
                            console.log('支付过程失败');
                            util.showErrorToast('支付失败');
                        },
                        complete: function (res) {
                            console.log('支付过程结束');
                        }
                    });
                }
            });
        }
    }
};
</script>
<style>
@import './payResult.css';
</style>
